import { Provider, observer } from 'mobx-react';
import React from 'react';
import ReactDOM from 'react-dom';
import { renderRoutes } from 'react-router-config';
import { HashRouter } from 'react-router-dom';
import Cookies from 'js-cookie';

import routes from './config/routes';
import stores from './stores';
import './styles/index.scss';
import { fetchUserInfo, fetchUserMenu } from './api';

class App extends React.Component<{}, {}>{

  componentDidMount() {
    const userLoginId = Cookies.get('ec_userLoginId')
    const activeMenuId = Cookies.get('_admin_active_menu_id_');

    if (userLoginId) {
      fetchUserInfo().then(result => {
        stores.userStore.setUserInfo(result.data);
      });

      fetchUserMenu('CONTROL_CENTER').then(result => {
        console.log('菜单', result);
        if (result.data) {
          stores.menuStore.setMenu(result.data);
          stores.menuStore.setActiveMenuId(activeMenuId || 'INQUIRY_TRACK')
        }
      })

      fetchUserMenu('STORE_MANAGER').then(result => {
        console.log('菜单', result);
      })
    } else {
      window.location.href = '/passport/login';
    }
  }

  render() {
    return (
      <HashRouter>
        <Provider {...stores}>
          {renderRoutes(routes)}
        </Provider>
      </HashRouter>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
